﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HerGoS.ControlsUI;
using HerGoS.Viaticos.Engine;
using HerGoS.WebControlExtend;

namespace HerGoS.WebUI.Viaticos.TipoTransporte
{
    public partial class TipoTransporte_Listado :PageBase
    {
        #region Propiedades
        public List<HerGoS.Viaticos.Entities.Transporte> Transportes
        {
            get
            {
                if (ViewState["Transportes"] != null)
                    return (List<HerGoS.Viaticos.Entities.Transporte>)ViewState["Transportes"];
                return null;
            }
            set
            {
                ViewState["Transportes"] = value;
            }

        }
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Initialize();
                LoadGridTransporte(false);
            }
        }

        void Initialize()
        {
            this.GridTransporte.Settings(new GridViewExtBasic());
            this.btnNuevo.Settings(new ButtonExtText());
            this.btnRefresh.Settings(new ButtonExtText());
        }
        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            LoadGridTransporte(false);
        }

        protected void btnBorrar_Click(object sender, EventArgs e)
        {
            ImageButton btn = (ImageButton)sender;
            int index = Convert.ToInt32(btn.CommandArgument);
            int id = Convert.ToInt32(GridTransporte.DataKeys[index].Value);


            HerGoS.Viaticos.Entities.Transporte transporte = Transportes.Find(c => c.OID == id);
            Transportes.Remove(transporte);

            GestorTransporte.Eliminar(transporte);
            LoadGridTransporte(true);

        }
        
        private void LoadGridTransporte(bool cache)
        {
            if (!cache)
                Transportes = GestorTransporte.Consultar();

            this.GridTransporte.DataSource = Transportes;
            this.GridTransporte.DataBind();
        }

        protected void GridTransporte_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButtonExt Editar = e.Row.FindControl("btnEditar") as ImageButtonExt;
                if (Editar != null)
                {
                    Editar.Settings(new ImageButtonExtEdit());
                    int oid = DataBinder.Eval(e.Row.DataItem, "OID").ToInteger();
                    Editar.OnClientClick = "Editar(" + oid.ToString() + "); return false;";
                }

                ImageButtonExt Borrar = e.Row.FindControl("btnBorrar") as ImageButtonExt;
                if (Borrar != null)
                {
                    Borrar.Settings(new ImageButtonExtDelete());
                }

            }
        }

        protected void GridTransporte_PageIndexChanging(Object sender, GridViewPageEventArgs e)
        {
            GridTransporte.PageIndex = e.NewPageIndex;
            LoadGridTransporte(true);
        }

    }
}